﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Framework;

namespace TjZheng.EasyUI
{
    public static partial class EasyUIExtension
    {
        /// <summary>
        /// CheckBox列表（空值验证）
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">input的name属性</param>
        /// <param name="value">input的value属性</param>
        /// <param name="isRequired">是否必选</param>
        /// <param name="remoteUrl">远程地址</param>
        /// <param name="htmlAttributes">Html属性</param>
        /// <param name="loadjs">加载脚本</param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, object value, bool isRequired, string remoteUrl, object htmlAttributes = null, string loadjs = "LoadCheckBoxList")
        {
            StringBuilder objStringBuilder = new StringBuilder();
            IDictionary<string, object> objHtmlAttributes = htmlAttributes.CreateValueDictionary();
            if (objHtmlAttributes.IsNull())
            {
                objHtmlAttributes = new Dictionary<string, object>();
            }
            string selector = name.Replace(".", "_");
            objHtmlAttributes["id"] = selector;
            objStringBuilder.AppendFormat("<div {0}>", objHtmlAttributes.Select(keyvalue => string.Format("{0}=\"{1}\"", keyvalue.Key, keyvalue.Value)).JoinString(" "));
            objStringBuilder.AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", name, value);
            objStringBuilder.Append("</div>");
            objStringBuilder.AppendFormat("<script type=\"text/javascript\">{0}(\"{1}\",\"{2}\",{3},\"{4}\");</script>", loadjs, selector.FormatSelf("#{0}"), value, isRequired.ToString().ToLower(), remoteUrl);
            return MvcHtmlString.Create(objStringBuilder.ToString());
        }
    }
}
